<?php
class Tourism_order_reviews_model extends CI_Model
{
    private $table = 'tourism_order_reviews';
    
    public function find()
    {
        return $this->db->get($this->table);
    }
    
    public function total($search_param=array())
    {
        if (!empty($search_param['order_id'])) {
            $this->db->where('order_id', $search_param['order_id']);
        }
        if (!empty($search_param['goods_name'])) {
            $this->db->like('goods_name', $search_param['goods_name']);
        }
        if (!empty($search_param['user_name'])) {
            $this->db->where('user_name', $search_param['user_name']);
        }
        if (!empty($search_param['status'])) {
            $this->db->where('status', $search_param['status']);
        }
        return $this->db->count_all_results($this->table);
    }
    
    public function page_list($page_num, $num, $search_param=array())
    {
        if (!empty($search_param['order_id'])) {
            $this->db->where('order_id', $search_param['order_id']);
        }
        if (!empty($search_param['goods_name'])) {
            $this->db->like('goods_name', $search_param['goods_name']);
        }
        if (!empty($search_param['user_name'])) {
            $this->db->where('user_name', $search_param['user_name']);
        }
        if (!empty($search_param['status'])) {
            $this->db->where('status', $search_param['status']);
        }
        $this->db->order_by('id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get($this->table);
    }
    
    public function updateTourismReviews($postData)
    {
        $data = array(
            'status'       => $postData['status'],
            'review_score' => $postData['review_score'],
            'user_content' => $postData['user_content'],
            'updated_at'   => date('Y-m-d H:i:s', time())
        );
        $this->db->where('id', $postData['id']);
        return $this->db->update($this->table, $data);
    }
    
    public function updateStatus($getData)
    {
    	 return $this->db->update($this->table,array('status'=>$getData['status'],'updated_at'=>date('Y-m-d H:i:s', time())),array('id'=>$getData['id']));
    }
    
    /**
     * 批量审核通过还是拒绝
     * @param unknown $reviewIdArray
     * @param unknown $status
     */
    public function setBatchStatus($reviewIdArray, $status) {
    	
        $data['status'] = $status;
        $data['updated_at'] = date('Y-m-d H:i:s', time());
        $this->db->where_in('id',$reviewIdArray);
        return $this->db->update($this->table,$data);
    }
    
    public function getproductReviewsById($product_id)
    {
        $this->db->where('product_id', $product_id);
        return $this->db->get($this->table);
    }
    
    public function findById($id)
    {
        $this->db->where('id', $id);
        return $this->db->get($this->table);
    }
    
    public function validateProductReviews($order_id, $product_id)
    {
        $this->db->where('order_id', $order_id);
        $this->db->where('product_id', $product_id);
        $this->db->where('uid', $this->session->userdata('ACT_UID'));
        return $this->db->count_all_results($this->table);
    }
}